package com.zkca.xf.presenter;


import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.zkca.xf.base.BasePresenter;
import com.zkca.xf.manager.DataManager;
import com.zkca.xf.model.javabean.VideoiList;
import com.zkca.xf.view.VideoView;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by li on 2018/8/21.
 */

public class VideoPersenter extends BasePresenter {
    private VideoView videoView;
    private DataManager dataManager;

    public VideoPersenter(LifecycleProvider<ActivityEvent> provider, VideoView videoView) {
        super(provider);
        dataManager = DataManager.getInstance();
        this.videoView = videoView;
    }

    public void getVideoList(Map<String, String> map) {
        videoView.showProgressDialog();
        dataManager.getvideoList(map)
                .subscribeOn(Schedulers.io())// 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread())// UI线程处理返回接口
                .compose(getProvider().<VideoiList>bindUntilEvent(ActivityEvent.DESTROY))// onDestroy取消订阅
                .subscribe(new DefaultObserver<VideoiList>() {
                    @Override
                    public void onNext(VideoiList videoiList) {
                        videoView.getVideolist(videoiList);
                        videoView.hideProgressDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        videoView.showError(e.getMessage());
                        videoView.hideProgressDialog();
                    }

                    @Override
                    public void onComplete() {
                        videoView.hideProgressDialog();
                    }
                });
    }

}
